package com.xiao.gulimall.product.service.impl;

import com.xiao.gulimall.product.dao.BrandDao;
import com.xiao.gulimall.product.dao.CategoryDao;
import com.xiao.gulimall.product.entity.*;
import com.xiao.gulimall.product.feing.EsFegin;
import com.xiao.gulimall.product.service.*;
import com.xiao.gulimall.product.vo.SkuEsModel;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiao.common.utils.PageUtils;
import com.xiao.common.utils.Query;

import com.xiao.gulimall.product.dao.SpuInfoDao;


@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {

    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private ProductAttrValueService productAttrValueService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private EsFegin esFegin;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                new QueryWrapper<SpuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public SpuInfoEntity getSpuInfoBySkuId(Long skuId) {

        return null;
    }
    //完成上架
    @Override
    public void up(Long spuId) {
        //查询当前spuid对应的所有sku的信息，品牌名称
        List<SkuInfoEntity> skuInfoEntities = skuInfoService.getSkusBySpuId(spuId);
        //TODO 根据spu查出当前sku的所有可以被用来检索的规格属性
        //这是查询的规格属性
        QueryWrapper<ProductAttrValueEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_id",spuId);
        List<ProductAttrValueEntity> list = productAttrValueService.list(queryWrapper);
        //把获取到属性ID
        List<SkuEsModel.Attr> attrs = list.stream().map(m -> {
            SkuEsModel.Attr attr = new SkuEsModel.Attr();
            BeanUtils.copyProperties(m, attr);
            return attr;
        }).collect(Collectors.toList());
        //最终上架到es的list是List<SkuEsModelvo>
        List<SkuEsModel> esModelList = skuInfoEntities.stream().map(attr -> {
            SkuEsModel skuEsModel = new SkuEsModel();
            BeanUtils.copyProperties(attr,skuEsModel);
            //skuprice
            skuEsModel.setSkuPrice(attr.getPrice());
            skuEsModel.setSkuImg(attr.getSkuDefaultImg());
            skuEsModel.setHotScore(0L);
            BrandEntity brandEntity = brandDao.selectById(attr.getBrandId());
            skuEsModel.setBrandImg(brandEntity.getLogo());
            skuEsModel.setBrandName(brandEntity.getName());
            CategoryEntity categoryEntity = categoryDao.selectById(attr.getCatalogId());
            skuEsModel.setCatalogName(categoryEntity.getName());
            skuEsModel.setAttrs(attrs);
            return skuEsModel;
        }).collect(Collectors.toList());
        esFegin.up(esModelList);
    }

}